package com.asohofrucol.web;

import java.util.HashMap;
import java.util.Map;

import javax.faces.component.UIOutput;
import javax.faces.event.AjaxBehaviorEvent;

import org.springframework.beans.factory.annotation.Autowired;

import com.asohofrucol.dto.AsohofrucolSessionDTO;
import com.asohofrucol.dto.ParameterLocationDTO;
import com.asohofrucol.dto.ParameterSiteDTO;
import com.asohofrucol.service.ParameterService;

/**
 * 
 * @author iván
 * 
 *         Esta clase contiene las listas que se muestran en los combos de
 *         localizacion ademas cargan paulatinamente una caché con los datos de
 *         cada departamento
 * 
 */
public class LocationController {
	@Autowired
	private ParameterService parameterService;
	@Autowired
	private ParameterLocationDTO cacheLocation;
	@Autowired
	private AsohofrucolSessionDTO aSession;

	private Map<String, String> departments;
	private Map<String, String> municipalities;
	private Map<String, String> villages;

	private String selectedDepartment = "52";
	private String selectedMunicipality = "52001";
	private String selectedVillage = "52001033";

	public static String COL = "col";
	private boolean farmLocation;

	public LocationController() {
		System.out.println("LocationController");
		departments = new HashMap<String, String>();
		municipalities = new HashMap<String, String>();
		villages = new HashMap<String, String>();
	}

	public void toogleDeparment(AjaxBehaviorEvent event) {
		System.out.println("toogleDeparment");
		this.selectedDepartment = (String) ((UIOutput) event.getSource())
				.getValue();
		this.obtainMunicipalities(this.selectedDepartment);
	}

	public void toogleMunicipality(AjaxBehaviorEvent event) {
		System.out.println("toogleMunicipality");
		this.selectedMunicipality = (String) ((UIOutput) event.getSource())
				.getValue();
		this.obtainVillages(this.selectedMunicipality);
	}

	public void toogleVillage(AjaxBehaviorEvent event) {
		System.out.println("toogleVillage");
		this.selectedVillage = (String) ((UIOutput) event.getSource())
				.getValue();
	}

	public void obtainDepartments() {
		if (cacheLocation.getDepartments().containsKey(COL)) {
			this.departments = cacheLocation.getDepartments().get(COL)
					.getOptions();
		} else {
			System.out.println("Loading Items [Departments]    ----  ");
			ParameterSiteDTO departmentOptions = parameterService
					.getDepartaments();
			this.departments = departmentOptions.getOptions();
			cacheLocation.getLocationDesc().putAll(
					departmentOptions.getDescriptions());
			this.putCacheDepartments(COL, this.departments);
		}
	}

	public void obtainMunicipalities(String codDepartament) {
		System.out.println("obtainMunicipalities ---- code: " + codDepartament);
		if (cacheLocation.getMunicipalities().containsKey(codDepartament)) {
			this.municipalities = cacheLocation.getMunicipalities()
					.get(codDepartament).getOptions();
		} else {
			System.out.println("Loading Items [Municipalities]    ----  ");
			ParameterSiteDTO municipalityOptions = parameterService
					.getMunicipalitysByDepartament(codDepartament);
			this.municipalities = municipalityOptions.getOptions();
			cacheLocation.getLocationDesc().putAll(
					municipalityOptions.getDescriptions());
			this.putCacheMunicipalities(codDepartament, this.municipalities);

		}
	}

	public void obtainVillages(String codMunicipality) {
		System.out.println("obtainVillages ---- code: " + codMunicipality);
		if (cacheLocation.getVillages().containsKey(codMunicipality)) {
			this.villages = cacheLocation.getVillages().get(codMunicipality)
					.getOptions();
		} else {
			System.out.println("Loading Items [Villages]    ----  ");
			ParameterSiteDTO villageOptions = parameterService
					.getVillagesByMunicipality(codMunicipality);
			this.villages = villageOptions.getOptions();
			cacheLocation.getLocationDesc().putAll(
					villageOptions.getDescriptions());
			this.putCacheVillages(codMunicipality, this.villages);
		}
	}

	public void putCacheDepartments(String key,
			Map<String, String> municipalities) {
		ParameterSiteDTO cacheMunicipality = new ParameterSiteDTO();
		cacheMunicipality.setOptions(municipalities);
		cacheLocation.getMunicipalities().put(key, cacheMunicipality);
	}

	public void putCacheMunicipalities(String key,
			Map<String, String> municipalities) {
		ParameterSiteDTO cacheMunicipality = new ParameterSiteDTO();
		cacheMunicipality.setOptions(municipalities);
		cacheLocation.getMunicipalities().put(key, cacheMunicipality);
	}

	public void putCacheVillages(String key, Map<String, String> villages) {
		ParameterSiteDTO cacheVillage = new ParameterSiteDTO();
		cacheVillage.setOptions(villages);
		cacheLocation.getVillages().put(key, cacheVillage);
	}

	public String getSelectedDepartmentDesc() {
		if (departments.containsKey(this.selectedDepartment)) {
			return this.departments.get(this.selectedDepartment);
		}
		return "";
	}

	public String getSelectedMunicipalityDesc() {
		if (municipalities.containsKey(selectedMunicipality)) {
			return this.municipalities.get(this.selectedMunicipality);
		}
		return "";
	}

	public String getSelectedVillageDesc() {
		if (villages.containsKey(this.selectedVillage)) {
			return this.villages.get(this.selectedVillage);
		}
		return "";
	}

	public Map<String, String> getDepartments() {
		if (cacheLocation != null && this.departments.isEmpty()) {
			System.out.println("departments is empty");
			this.obtainDepartments();
			if (aSession.getSelectedRuatRegister() != null) {
				if (farmLocation) {
					this.selectedDepartment = aSession
							.getSelectedRuatRegister().getFarmDepartament();
					this.selectedMunicipality = aSession
							.getSelectedRuatRegister().getFarmMunicipality();
					this.selectedVillage = aSession.getSelectedRuatRegister()
							.getFarmVillage();
				} else {
					this.selectedDepartment = aSession
							.getSelectedRuatRegister().getDepartament();
					this.selectedMunicipality = aSession
							.getSelectedRuatRegister().getMunicipality();
					this.selectedVillage = aSession.getSelectedRuatRegister()
							.getVillage();
				}
				if (this.selectedDepartment == ""
						&& this.selectedMunicipality == ""
						&& this.selectedVillage == "") {
					selectedDepartment = "52";
					selectedMunicipality = "52001";
					selectedVillage = "52001033";
				}
			}

			this.obtainMunicipalities(this.selectedDepartment);
			this.obtainVillages(this.selectedMunicipality);
		}
		return departments;
	}

	public void setDepartments(Map<String, String> departments) {
		this.departments = departments;
	}

	public Map<String, String> getMunicipalities() {
		return municipalities;
	}

	public void setMunicipalities(Map<String, String> municipalities) {
		this.municipalities = municipalities;
	}

	public Map<String, String> getVillages() {
		return villages;
	}

	public void setVillages(Map<String, String> villages) {
		this.villages = villages;
	}

	public String getSelectedDepartment() {
		return selectedDepartment;
	}

	public void setSelectedDepartment(String selectedDepartment) {
		this.selectedDepartment = selectedDepartment;
	}

	public String getSelectedMunicipality() {
		return selectedMunicipality;
	}

	public void setSelectedMunicipality(String selectedMunicipality) {
		this.selectedMunicipality = selectedMunicipality;
	}

	public String getSelectedVillage() {
		return selectedVillage;
	}

	public void setSelectedVillage(String selectedVillage) {
		this.selectedVillage = selectedVillage;
	}

	public ParameterService getParameterService() {
		return parameterService;
	}

	public void setParameterService(ParameterService parameterService) {
		this.parameterService = parameterService;
	}

	public ParameterLocationDTO getCacheLocation() {
		return cacheLocation;
	}

	public void setCacheLocation(ParameterLocationDTO cacheLocation) {
		this.cacheLocation = cacheLocation;
	}

	public boolean isFarmLocation() {
		return farmLocation;
	}

	public void setFarmLocation(boolean farmLocation) {
		this.farmLocation = farmLocation;
	}

}
